package com.jikangjun.disk.myshare.service.impl;

import com.jikangjun.disk.mybatis.flexigrid.PagingEntity;
import com.jikangjun.disk.mybatis.flexigrid.TblResponseEntity;
import com.jikangjun.disk.myshare.entity.SearchShareEntity;
import com.jikangjun.disk.myshare.entity.ShareEntity;
import com.jikangjun.disk.myshare.repository.ShareDao;
import com.jikangjun.disk.myshare.service.ShareService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 分享服务实现类
 */
@Service
public class ShareServiceImpl implements ShareService {
    
    @Resource
    private ShareDao shareDao;
    
    @Override
    public TblResponseEntity<ShareEntity> queryShareInfoListByPage(SearchShareEntity entity, PagingEntity paging) {
        TblResponseEntity<ShareEntity> tblResponseEntity = new TblResponseEntity<>();
        Integer total = shareDao.queryShareListTotal(entity);
        tblResponseEntity.setTotal(total);
        
        paging.deal(ShareEntity.class, total);
        tblResponseEntity.setPage(paging.getPage());
        List<ShareEntity> list = shareDao.queryShareInfoListByPage(entity, paging);
        tblResponseEntity.setRawRecords(list);
        
        tblResponseEntity.deal();
        return tblResponseEntity;
    }
    
    @Override
    public ShareEntity queryShareInfoById(String shareId) {
        return shareDao.queryShareInfoById(shareId);
    }
    
    @Override
    public ShareEntity queryShareInfoByLink(String shareLink) {
        return shareDao.queryShareInfoByLink(shareLink);
    }
    
    @Override
    public ShareEntity createShare(ShareEntity entity) {
        // 生成分享ID
        entity.setShareId(UUID.randomUUID().toString().replace("-", ""));
        
        // 生成分享链接
        if (entity.getShareLink() == null || entity.getShareLink().isEmpty()) {
            entity.setShareLink(generateShareLink());
        }
        
        // 生成分享码
        if (entity.getShareCode() == null || entity.getShareCode().isEmpty()) {
            entity.setShareCode(generateShareCode());
        }
        
        // 设置分享时间
        if (entity.getShareTime() == null) {
            entity.setShareTime(new Date());
        }
        
        // 设置初始状态
        entity.setStatus(1);
        entity.setDownloadCount(0);
        entity.setViewCount(0);
        
        shareDao.addShareInfo(entity);
        return entity;
    }
    
    @Override
    public void updateShareInfo(ShareEntity entity) {
        shareDao.updateShareInfo(entity);
    }
    
    @Override
    public void cancelShare(String shareId) {
        shareDao.updateShareStatus(shareId, 0);
    }
    
    @Override
    public void cancelShareBatch(String[] shareIds) {
        for (String shareId : shareIds) {
            shareDao.updateShareStatus(shareId, 0);
        }
    }
    
    @Override
    public void deleteShare(String shareId) {
        shareDao.deleteShareById(shareId);
    }
    
    @Override
    public void deleteShareBatch(String[] shareIds) {
        shareDao.deleteShareByIds(shareIds);
    }
    
    @Override
    public void updateDownloadCount(String shareId) {
        shareDao.updateDownloadCount(shareId);
    }
    
    @Override
    public void updateViewCount(String shareId) {
        shareDao.updateViewCount(shareId);
    }
    
    @Override
    public String generateShareLink() {
        // 生成唯一的分享链接
        return "https://disk.example.com/share/" + UUID.randomUUID().toString().replace("-", "");
    }
    
    @Override
    public String generateShareCode() {
        // 生成6位数字分享码
        return String.format("%06d", (int)(Math.random() * 1000000));
    }
} 